<?php
function distance($lat1, $lon1, $lat2, $lon2, $unit){ 

	$theta = $lon1 - $lon2;
  	$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  	$dist = acos($dist);
  	$dist = rad2deg($dist);
  	$miles = $dist * 60 * 1.1515;
  	$unit = strtoupper($unit);

  	if ($unit == "K"){
    	return ($miles * 1.609344);
  	} 
  	else if ($unit == "N"){
      	return ($miles * 0.8684);
    } 
    else{
        return $miles;
    }
}

echo distance(32.9697, -96.80322, 29.46786, -98.53506, "k")." kilometers";
	
	
function Sec2Time($time){
	if(is_numeric($time)){
		$value = array(
			"years" => 0, "days" => 0, "hours" => 0,
			"minutes" => 0, "seconds" => 0,
		);
		if($time >= 31556926){
			$value["years"] = floor($time/31556926);
			$time = ($time%31556926);
		}
		if($time >= 86400){
			$value["days"] = floor($time/86400);
			$time = ($time%86400);
		}
		if($time >= 3600){
			$value["hours"] = floor($time/3600);
		  	$time = ($time%3600);
		}
		if($time >= 60){
		  	$value["minutes"] = floor($time/60);
		  	$time = ($time%60);
		}
		$value["seconds"] = floor($time);
	
		return (array) $value;
	}
	else{
		return (bool) FALSE;
	}
}	
	

echo '<pre>';
var_dump(Sec2Time('98600'));


function forceDownload($file){
    $file_name = $file;
    $mime = 'application/force-download';
	header('Pragma: public'); 	// required
	header('Expires: 0');		// no cache
	header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
	header('Cache-Control: private',false);
	header('Content-Type: '.$mime);
	header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
	header('Content-Transfer-Encoding: binary');
	header('Connection: close');
	readfile($file_name);		// push it out
	exit();
}

//forceDownload('phpinfo.php');

function getLatLong($address){
	if (!is_string($address))die("All Addresses must be passed as a string");
	$_url = sprintf('http://maps.google.com/maps?output=js&q=%s',rawurlencode($address));
	$_result = false;
	if($_result = file_get_contents($_url)) {
		if(strpos($_result,'errortips') > 1 || strpos($_result,'Did you mean:') !== false) return false;
		preg_match('!center:\s*{lat:\s*(-?\d+\.\d+),lng:\s*(-?\d+\.\d+)}!U', $_result, $_match);
		$_coords['lat'] = $_match[1];
		$_coords['long'] = $_match[2];
	}
	return $_coords;
}

echo '<pre>';
//var_dump(getLatLong('Indaial, Santa Catarina, Brasil'));


/**
 * Remover HTML de string
 */
//$text = strip_tags($input, "");


/**
 * Extrair emails de uma string
 */
function extract_emails($str){
    // This regular expression extracts all emails from a string:
    $regexp = '/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
    preg_match_all($regexp, $str, $m);

    return isset($m[0]) ? $m[0] : array();
}

$test_string = 'This is a test string...

        test1@example.org

        Test different formats:
        test2@example.org;
        <a href="test3@example.org">foobar</a>
        <test4@example.org>

        strange formats:
        test5@example.org
        test6[at]example.org
        test7@example.net.org.com
        test8@ example.org
        test9@!foo!.org

        foobar
';

print_r(extract_emails($test_string));












	
?>